package edu.gatech.coc.betameisters.userinterfaces;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import edu.gatech.coc.betameisters.infoholders.Appointment;
import edu.gatech.coc.betameisters.infoholders.Constants;
import edu.gatech.coc.betameisters.main.MainPanel;
import edu.gatech.coc.betameisters.people.Doctor;
import edu.gatech.coc.betameisters.people.Nurse;
import edu.gatech.coc.betameisters.people.Person;

import javax.swing.DefaultComboBoxModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JTextPane;

public class EditAppointments extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7431648330292535848L;
	@SuppressWarnings("unused")
	private MainPanel myParent;

	/**
	 * Create the panel.
	 */
	public EditAppointments(final Appointment thisAppointment,final MainPanel p) {
		myParent = p;
		setLayout(null);

		JLabel lblAppointments = new JLabel("Appointments");
		lblAppointments.setBounds(169, 11, 82, 14);
		add(lblAppointments);

		JButton btnSubmit = new JButton("Submit/Create Appointment");
		
		btnSubmit.setBounds(169, 266, 172, 23);
		add(btnSubmit);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Person aPer = myParent.getUser();
				if(aPer.getUserType() == Constants.UserType.DOCTOR){
					myParent.setDoctor((Doctor) aPer);
				}
				else{
					myParent.setNurse((Nurse) aPer);
				}
			}
		});
		btnCancel.setBounds(351, 266, 89, 23);
		add(btnCancel);

		JLabel lblPatient = new JLabel("Patient:");
		lblPatient.setBounds(10, 33, 46, 14);
		add(lblPatient);

		String patientUName = "";
		if(thisAppointment.getPatient() != null){
			patientUName = thisAppointment.getPatient().getUserID();
		}
		JLabel lblPatientName = new JLabel(patientUName);
		lblPatientName.setBounds(55, 33, 55, 14);
		add(lblPatientName);

		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "01", "02",
				"03", "04", "05", "06", "07", "08", "09", "10", "11",
				"12" }));
		comboBox.setBounds(10, 112, 46, 20);
		add(comboBox);

		JLabel lblMonth = new JLabel("Month");
		lblMonth.setBounds(10, 87, 46, 14);
		add(lblMonth);

		JLabel lblDay = new JLabel("Day");
		lblDay.setBounds(65, 86, 46, 14);
		add(lblDay);

		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] { "01", "02",
				"03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
				"13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
				"23", "24", "25", "26", "27", "28", "29", "30", "31" }));
		comboBox_1.setBounds(66, 112, 44, 20);
		add(comboBox_1);

		JLabel lblYear = new JLabel("Year");
		lblYear.setBounds(121, 87, 46, 14);
		add(lblYear);

		final JComboBox comboBox_2 = new JComboBox();
		comboBox_2.setModel(new DefaultComboBoxModel(new String[] { "2011",
				"2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019",
				"2020" }));
		comboBox_2.setBounds(120, 112, 65, 20);
		add(comboBox_2);

		JLabel lblHour = new JLabel("Hour");
		lblHour.setBounds(10, 155, 46, 14);
		add(lblHour);

		final JComboBox comboBox_3 = new JComboBox();
		comboBox_3.setModel(new DefaultComboBoxModel(new String[] {"07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17"}));
		comboBox_3.setBounds(10, 176, 46, 20);
		add(comboBox_3);

		final JComboBox comboBox_4 = new JComboBox();
		comboBox_4.setModel(new DefaultComboBoxModel(new String[] { "00", "15",
				"30", "45" }));
		comboBox_4.setBounds(65, 176, 45, 20);
		add(comboBox_4);

		JLabel lblMinute = new JLabel("Minute");
		lblMinute.setBounds(64, 155, 46, 14);
		add(lblMinute);
		
		final JTextPane txtDescript = new JTextPane();
		txtDescript.setBounds(225, 81, 215, 115);
		add(txtDescript);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setBounds(225, 55, 82, 14);
		add(lblDescription);

		//Submit Button
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int year = Integer.parseInt((String) comboBox_2.getSelectedItem());
				int month = Integer.parseInt((String) comboBox.getSelectedItem());
				int day = Integer.parseInt((String) comboBox_1.getSelectedItem());
				int sHour = Integer.parseInt((String) comboBox_3.getSelectedItem());
				int sMinute = Integer.parseInt((String) comboBox_4.getSelectedItem());
				GregorianCalendar start = new GregorianCalendar(year, month - 1, day, sHour, sMinute, 0);
				GregorianCalendar end = new GregorianCalendar(year, month - 1, day, sHour, sMinute + 14, 0);
				System.out.println(thisAppointment.getContact());
				Appointment thisAppoint = new Appointment((Person) p.getLocal(),(Person) p.getUser(), start, end, txtDescript.getText());
				
				try {
					thisAppoint.update();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}
